package com.hd.trans.dialog

import android.view.LayoutInflater
import com.hd.trans.R
import com.hd.trans.TranslationInterior
import com.hd.trans.binding.setOnDebouncedClickListener
import com.hd.trans.databinding.PopupSimuRecordingSettingBinding
import com.hd.trans.extension.translationXAnimation
import com.hd.trans.framework.dialog.CstBindPop
import com.hd.trans.ui.activity.customize.SimulTranslateActivity
import com.hd.trans.utils.ToastUtils

class SimuRecordingSettingPopup(private val mParent: SimulTranslateActivity): CstBindPop<PopupSimuRecordingSettingBinding>(
    LayoutInflater.from(mParent).inflate(R.layout.popup_simu_recording_setting, null), true
) {

    private var tempSettings = mParent.audioSourceSettings.copy()

    var onSure: ((Boolean,Boolean) -> Unit)? = null

    override val dimAmount: Float
        get() = 0f

    init {

        mDataBinding.apply {
            btnClose.setOnDebouncedClickListener {
                dismiss()
            }

            btnSure.setOnDebouncedClickListener {
                dismiss()
                if (mParent.audioSourceSettings != tempSettings){
                    mParent.audioSourceSettings.put(tempSettings)
                    onSure?.invoke(tempSettings.isRecordMic,tempSettings.isRecordSys)
                }
            }

            swMic.setOnClickListener {
                val enable = !tempSettings.isRecordMic

                TranslationInterior.getTrackerListener().click(
                    "同声传译",
                    mParent.mTabName,
                     "收音设置-麦克风声音",
                )

                if (!enable && !tempSettings.isRecordSys){
                    // 不能同时关闭麦克风和系统声音
                    ToastUtils.showNormal("至少开启一项收音设置")
                    return@setOnClickListener
                }
                setRecordMicEnable(enable,true)
            }

            swSys.setOnClickListener {
                val enable = !tempSettings.isRecordSys

                TranslationInterior.getTrackerListener().click(
                    "同声传译",
                    mParent.mTabName,
                    "收音设置-系统声音"
                )

                if (!enable && !tempSettings.isRecordMic){
                    // 不能同时关闭麦克风和系统声音
                    ToastUtils.showNormal("至少开启一项收音设置")
                    return@setOnClickListener
                }

                if (enable){
                    mParent.requestMediaProjection {
                        if (it != null){
                            setRecordSysEnable(enable,true)
                        }
                    }
                }
                else{
                    setRecordSysEnable(enable,true)
                }

            }


        }

    }

    private fun setRecordMicEnable(enable:Boolean,fromUser:Boolean){
        tempSettings.isRecordMic = enable
        mDataBinding.swMic.isSelected = enable

        val translationX = if (enable) mContext.resources.getDimensionPixelSize(R.dimen.dp_22dp).toFloat() else 0f
        if (fromUser){
            mDataBinding.thumbMic.translationXAnimation(translationX,150)
        }
        else{
            mDataBinding.thumbMic.translationX = translationX
        }
    }

    private fun setRecordSysEnable(enable:Boolean,fromUser:Boolean){
        tempSettings.isRecordSys = enable
        mDataBinding.swSys.isSelected = enable

        val translationX = if (enable) mContext.resources.getDimensionPixelSize(R.dimen.dp_22dp).toFloat() else 0f
        if (fromUser){
            mDataBinding.thumbSys.translationXAnimation(translationX,150)
        }
        else{
            mDataBinding.thumbSys.translationX = translationX
        }
    }

    fun show(){
        showFromBottom()
        tempSettings = mParent.audioSourceSettings.copy()
        mDataBinding.swMic.post {
            setRecordMicEnable(tempSettings.isRecordMic,false)
            setRecordSysEnable(tempSettings.isRecordSys,false)
        }
    }


}